/*
知识点：
1.路由懒加载。
2.路由模糊匹配转跳。
3.路由导航守卫(判断token是否登陆、权限)
 */
import Vue from 'vue'
import Router from 'vue-router'
//import Home from './views/Home.vue'

Vue.use(Router)


/*
下面代码用以解决重复执行相同路由跳转时报错的问题。
重写vue-router的push方法，把报错捕获一下不处理。
据说提高vue-router的版本即可解决。
*/
const originalPush = Router.prototype.push
Router.prototype.push = function push(location) {
  return originalPush.call(this, location).catch(err => err)
}

export default new Router({
  mode: 'history',
  base: process.env.BASE_URL, //注意：BASE_URL是内置变量，无需在.env.xxx文件里配置，只需要在vue.config.js里加上publicPath: '/mytest'即可。
  routes: [
    {
      path: '/home',
      redirect:'/home/video', //当访问home路由页面时，转跳到导航菜单的第一个选项
      name: 'home',
      component: ()=>import('@/views/homepage'),
      children:[
        {
          path:'video',
          redirect:'/home/video/videofolder', //当访问video路由页面时，转跳到它的子路由videofolder
          name:'video',
          component:()=>import('@/components/content/video'),
          children:[
            {
              path:'videofolder',
              name:'videofolder',
              component:()=>import('@/components/content/video/videofolder')
            },
            {
              path:'videodetail',
              name:'videodetail',
              component:()=>import('@/components/content/video/videodetail')
            }
          ]
        },
        {
          path:'photo',
          name:'photo',
          component:()=>import('@/components/content/photo')
        },
        {
          path:'authority',
          name:'authority',
          component:()=>import('@/components/content/authority')
        },
        {
          path:'upload',
          name:'upload',
          component:()=>import('@/components/content/upload')
        }
      ]
    },
    {
      path: '/login',
      name: 'login',
      component: ()=>import('@/views/loginpage')
    },
    {
      path: '/test',
      name: 'test',
      component: ()=>import('@/views/testpage')
    },
    { //路由匹配规则，优先级从上到下。上面所有路由都匹配不到的，最后用 * 来模糊匹配，重定向到home页面
      path: '*',
      redirect: '/login'
    }
  ]
})
